#include "imagefusion.grpc.pb.h"
#include "imagefusion.pb.h"
#include <grpc++/server.h>
#include <grpc++/server_builder.h>
#include <grpc++/server_context.h>
#include <grpcpp/grpcpp.h>
#include <iostream>
#include <memory>
#include <sstream>
#include <string>

using com::hh::imagefusion::CameraInfo;
using com::hh::imagefusion::CameraInfoResponse;
using com::hh::imagefusion::FusionService;
using grpc::Channel;
using grpc::ClientContext;
using grpc::Status;

class FusionClient {
  public:
    FusionClient( std::shared_ptr<Channel> channel ) : stub_( FusionService::NewStub( channel ) ) {}
    
    std::string pushRequest( const std::string& ip,int port,const std::string& user,const std::string& passwd,const std::string& rtmp ) {
        CameraInfo request;
        request.set_cameraip( ip );
        request.set_cameraport( port );
        request.set_username( user );
        request.set_password( passwd );
        request.set_rtmpurl( rtmp );

        // Container for the data we expect from the server.
        CameraInfoResponse reply;

        // Context for the client. It could be used to convey extra information to
        // the server and/or tweak certain RPC behaviors.
        ClientContext context;

        // The actual RPC.
        Status status = stub_->pushCameraInfo( &context, request, &reply );

        // Act upon its status.
        if ( status.ok() ) {
            std::cout << reply.message() << std::endl;
            return reply.message();
        } else {
            std::cout << status.error_code() << ": " << status.error_message() << std::endl;
            return "RPC failed";
        }
    }
  private:
      std::unique_ptr<FusionService::Stub> stub_;
};

int main( int argc, char** argv ) {
    // Instantiate the client. It requires a channel, out of which the actual RPCs
    // are created. This channel models a connection to an endpoint (in this case,
    // localhost at port 50051). We indicate that the channel isn't authenticated
    // (use of InsecureChannelCredentials()).
    std::string rtmpip = argv[1];
    FusionClient greeter( grpc::CreateChannel( "localhost:50051", grpc::InsecureChannelCredentials() ) );
    std::string reply = greeter.pushRequest( "192.168.1.64",554,"admin","ad53937301","rtmp://"+rtmpip+"/live/stream" );
    std::cout << "Fusion received: " << reply << std::endl;
    return 0;
}